package com.api.devices.test;

import java.io.*;

public class HexDatFileReader {
    private static String filePath = "D:\\Project\\demo\\20250515210200.dat";
    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
//            try{
//                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            // 跳过第一行（可能是标题或注释）
            br.readLine();

            String line;
            while ((line = br.readLine()) != null) {
                // 移除可能的空格和分隔符
                String hexString = line.replaceAll("\\s+", "");

                // 将HEX字符串转换为字节数组
                byte[] bytes = hexStringToBytes(hexString);

                // 处理字节数据
                System.out.println("Line HEX: " + hexString);
                System.out.println("As bytes: " + java.util.Arrays.toString(bytes));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // HEX字符串转字节数组
    private static byte[] hexStringToBytes(String hexString) {
        if (hexString.length() % 2 != 0) {
            hexString = "0" + hexString;
        }

        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i += 2) {
            String hex = hexString.substring(i, i + 2);
            bytes[i / 2] = (byte) Integer.parseInt(hex, 16);
        }
        return bytes;
    }
}
